package com.api.regression.group.searchdocuments;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.DocumentApi;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class SearchDocuments extends AbstractApiTest {

	private static final long DEFAULT_TIME_DIFFERENCE = 100;

	@Value("#{groupsearch['user.searcher'].appId}")
	private String userUrn;

	@Value("#{groupsearch['user.searcher'].password}")
	private String userPassword;

	@Value("#{groupsearch[searchDocuments].sharedText}")
	private String sharedText;

	@Value("#{groupsearch[searchDocuments].notSharedText}")
	private String notSharedText;

	@Value("#{groupsearch['user.groupCreator'].appId}")
	private String userFilterUrn;

	@Value("#{groupsearch['group.searchDocuments'].urn}")
	private String groupFilterUrn;

	@Value("#{groupsearch['group.searchDocuments'].documentUrn}")
	private String dateCreationDocumentUrn;

	@Value("#{groupsearch[searchDocuments].cantShared}")
	private int cantSharedDocuments;

	@Value("#{groupsearch[searchDocuments].cantNotShared}")
	private int cantNotSharedDocuments;

	@Value("#{groupsearch[searchDocuments].cantUserShared}")
	private int cantUserSharedDocuments;

	@Value("#{groupsearch[searchDocuments].cantGroupShared}")
	private int cantGroupSharedDocuments;

	private long fromDate;

	private long toDate;

	@BeforeClass
	public void loginUser() throws ZyncroApiException {

		getApi().login(userUrn, userPassword);

		DocumentApi document = getApi().getService(IGroupApiService.class).getDocument(groupFilterUrn, dateCreationDocumentUrn).getObject();

		this.fromDate = document.getCreationTime() - DEFAULT_TIME_DIFFERENCE;
		this.toDate = document.getCreationTime() + DEFAULT_TIME_DIFFERENCE;
	}

	@Test
	public void testSearchSharedText() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		List<String> groups = null;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).searchDocuments(pageNumber, itemsPerPage,
				sharedText, groups, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantSharedDocuments);
	}

	@Test
	public void testSearchNotSharedText() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		List<String> groups = null;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).searchDocuments(pageNumber, itemsPerPage,
				notSharedText, groups, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantNotSharedDocuments);
	}

	@Test
	public void testSearchSharedTextFilteringUser() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;

		List<String> users = new ArrayList<String>();
		users.add(userFilterUrn);

		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		List<String> groups = null;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).searchDocuments(pageNumber, itemsPerPage,
				sharedText, groups, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantUserSharedDocuments);
	}

	@Test
	public void testSearchSharedTextFilteringGroup() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;

		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		List<String> groups = new ArrayList<String>();
		groups.add(groupFilterUrn);

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).searchDocuments(pageNumber, itemsPerPage,
				sharedText, groups, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantGroupSharedDocuments);
	}

	@Test
	public void testSearchSharedTextFilteringDate() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = String.valueOf(this.fromDate);
		String toDate = String.valueOf(this.toDate);
		boolean onlyResultSize = false;

		List<String> groups = null;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).searchDocuments(pageNumber, itemsPerPage,
				sharedText, groups, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), 1);
	}

	@Test
	public void testSearchSharedTextOnlySize() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = true;

		List<String> groups = null;

		PagedItemsResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).searchDocuments(pageNumber, itemsPerPage,
				sharedText, groups, users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantSharedDocuments);
		Assert.assertEquals(result.getElements().size(), 0);
	}
}
